<?php

class Application_Model_UserAccount extends Application_Model_DomainObject{
    
    protected $_id;
    
    protected $_money;
    
    protected $_score;
    
    protected static function getDb(){
        return new Application_Model_DbTable_UserAccount();
    }


    /**
     * 根据用户ID返回对应的帐户
     * @param int $userId
     * @return Application_Model_UserAccount 
     */
    public static function getByUserId($userId){
        return Application_Model_UserAccount::getDb()->getByUserId($userId);
    }
    
    /**
     * 返回当前用户的历史记录
     * @param 操作类型 $type 1、扣除余额，2、充值,3、扣除积分
     * @param type $dateStart
     * @param type $dateEnd
     * @return array 
     */
    public function getHistory($type,$dateStart,$dateEnd){
        return Application_Model_UserAccountHistory::search($this->Id,$type,$dateStart,$dateEnd);
    }
    
    public function getMoneyChange($dateStart,$dateEnd, $count, $offset){
        return Application_Model_UserAccountHistory::getMoneyChange($this->Id, $dateStart, $dateEnd, $count, $offset);
    }
    
    /**
     * 未处理代充值记录，状态为0或者5
     */
    public function getTopUpAlipay($id, $count, $offset)
    {
    	return Application_Model_topUpAlipay::getDB()->queryPage(array('userId'=>$id),'dateCreated desc',$count, $offset);    	
    }
    
    /**
     * 获取用户指定状态的代充值记录数
     * @param int $id
     * @param int $status
     */    
    public function getTopUpAlipayCount($id, $status)
    {
    	return Application_Model_topUpAlipay::getDB()->count(array('userId'=>$id,'status'=>$status));
    }
    
    /**
     * 获取用户最后一次代充值记录
     * @param int $id
     */
    public function getUserLastTopUpAlipay($id)
    {
    	return Application_Model_topUpAlipay::getDB()->queryPage(array('userId'=>$id),"dateCreated desc",1,0);
    }
    
    /**
     * 完成支付功能，从用户帐户扣除指定的金额，并记录操作记录，修改支付状态。
     * @param type $money
     * @param type $payObject 
     */
    public function pay($money,$payObject){
        try{
            $this->getDbTable()->beginTran();
            $history = new Application_Model_UserAccountHistory();
            $history->Type = Application_Model_UserAccountHistory::TYPE_DISMONEY;
            $history->BeforeMoney = $this->Money;
            $history->AfterMoney = $this->Money - $money;
            $history->Comment = $payObject->getPayName();
            $history->DateCreated = date('Y-m-d H:i:s');
            $history->RelaseId = $payObject->Id;
            $history->BeforeScore = $this->Score;
            
            $history->AfterScore = $this->Score;
            $history->AccountId = $this->Id;
            $history->save();
            
            $payObject->setPaid();
            $payObject->save();
            
            $this->Money -= $money;            
            $this->save();
            
            $this->getDbTable()->commit();
        }  catch (Exception $ex){
            $this->getDbTable()->rollBack();
            throw $ex;
        }
        
    }
    
    public function recharge($paybill){
        try{
            $this->getDbTable()->beginTran();
            $history = new Application_Model_UserAccountHistory();
            $history->Type = Application_Model_UserAccountHistory::TYPE_RECHARGE_MONEY;
            $history->BeforeMoney = $this->Money;
            $history->AfterMoney = $this->Money + $paybill->Money;
            $history->Comment = '充值成功';
            $history->DateCreated = date('Y-m-d H:i:s');
            $history->RelaseId = $paybill->Id;
            $history->BeforeScore = $this->Score;
            
            $history->AfterScore = $this->Score;
            $history->AccountId = $this->Id;
            $history->save();
            
            $paybill->Status = Application_Model_PayBill::PAY_STATUS_SUCCESS;
            $paybill->save();
            
            $this->Money += $paybill->Money;            
            $this->save();
            
            $this->getDbTable()->commit();
            
        }catch(Exception $ex){
            $this->getDbTable()->rollBack();
            throw $ex;
        }
    }
    
    public function changeMoney($money,$type,$desc){
        $history = new Application_Model_UserAccountHistory();
        $account =  $this;
        $history->BeforeMoney = $account->Money;
        $history->AfterMoney = $account->Money + $money;
        $history->BeforeScore = $account->Score;
        $history->AfterScore = $account->Score;
        $history->DateCreated = date('Y-m-d H:i:s');
        $history->Comment = $desc;
        $history->AccountId = $this->_id;
        $history->Type = $type;
        $account->Money += $money;
        $history->save();
        $this->save();
    }
}